package ci092.trabalho.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import ci092.trabalho.Professor;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RooWebScaffold(path = "professors", formBackingObject = Professor.class)
@RequestMapping("/professors")
@Controller
public class ProfessorController {
	
	
    @RequestMapping(method = RequestMethod.POST)
    public String create(@Valid Professor professor, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            uiModel.addAttribute("professor", professor);
            return "professors/create";
        }
        uiModel.asMap().clear();
        professor.persist();
        return "redirect:/professors/" + encodeUrlPathSegment(professor.getId().toString(), httpServletRequest);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String update(@Valid Professor professor, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            uiModel.addAttribute("professor", professor);
            return "professors/update";
        }
        uiModel.asMap().clear();
        professor.merge();
        return "redirect:/professors/" + encodeUrlPathSegment(professor.getId().toString(), httpServletRequest);
    }
 
}
